//wx-drawer
var common = require('../../utils/common.js')
var Bmob = require("../../utils/bmob.js");
var util = require('../../utils/util.js');
var app = getApp()
var curIndex  = 0 ;
var that;

Page({
  data: {
    my_nick: '不知道',
    my_sex: '0',
    my_avatar: '',
    dialog:false,
    buttonClicked: false, //是否点击跳转
    windowHeight: 0,
    windowWidth: 0,
    totalPublishCount : 0,
    totalJoinCount: 0,
    infoCount: 0,
    noticeCount: 0,
    menuList:[]
  },
  onHide:function(){
    this.setData({
      autoplay:false
    })
  },

  onLoad(t) {
    var that = this;
    try {
      let res = wx.getSystemInfoSync();
      that.setData({ windowWidth: res.windowWidth,
        my_nick:wx.getStorageSync('my_nick'),
        my_sex:wx.getStorageSync('my_sex'),
        my_avatar:wx.getStorageSync('my_avatar')
       })
      that.loadMenu();
    } catch (e) {
    }
  },

  loadMenu:function(){
    var menuList = [
      { title: '我的收藏', icon: 'favorite', url: '/pages/my/mycollection/mycollection' },
      { title: '我的消息', icon: 'message', url: '/pages/message/message' },
      { title: '我的记录', icon: 'save', url: '/pages/my/mylaunch/mylaunch' },
      { title: '意见反馈', icon: 'feedback', url: '/pages/my/issues/issues' },
      { title: '开发日志', icon: 'log', url: '/pages/my/logs/logs' },
      { title: '关于我们', icon: 'fm', url: '/pages/my/aboutus/aboutus' }
    ]
    if (new Date() > new Date("2018/06/06 20:00:00")) {
      menuList = [
      { title: '我的日报', icon: 'daily', url: '/pages/my/zhihu/index/index' },
      { title: '我的电影', icon: 'movie', url: '/pages/my/movie/movie' },
      { title: '2048', icon: 'game', url: '/pages/my/2048/2048' },
      { title: '综合', icon: 'fm', url: '/pages/my/news/index' },
      { title: '我的小说', icon: 'novel', url: '/pages/my/novel/search/search' },
      { title: '我的知识', icon: 'juejin', url: '/pages/my/juejin/juejin' },
      { title: '我的图片', icon: 'photo', url: '/pages/my/photo/explore/explore' },
      { title: '更多信息', icon: 'more', url: '/pages/my/more/more' },
      { title: '关于我们', icon: 'fm', url: '/pages/my/aboutus/aboutus' }
      ]
    }
    this.setData({ menuList: menuList});
  },

  onShow: function (e) {
    let self = this;

    var user_id = wx.getStorageSync('user_id')
    var me = new Bmob.User();
    me.id = user_id;
    
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowHeight1: res.windowHeight,
          windowWidth1: res.windowWidth,
          autoplay: true
        })
      }
    })
  },

 
  getUserInfoFun: function (r) {
    var that = this;
    wx.setStorageSync("userInfo", r.detail.userInfo);
    //调用API从本地缓存中获取数据
    try {
      var value = wx.getStorageSync('user_openid');
      var userInfo = r.detail.userInfo;
      if (value) {
      } else {
        console.log('执行login...')
        wx.login({
          success: function (res) {
            if (res.code) {
              Bmob.User.requestOpenId(res.code, {
                success: function (userData) {
                  var result = r.detail;
                  var userInfo = result.userInfo;
                  var nickName = userInfo.nickName;
                  var avatarUrl = userInfo.avatarUrl;
                  var sex = userInfo.gender;
                  Bmob.User.logIn(userData.openid, userData.openid, {
                    success: function (user) {
                      try {
                        wx.setStorageSync('user_openid', user.get('userData').openid)
                        wx.setStorageSync('user_id', user.id)
                        wx.setStorageSync('my_nick', user.get("nickname"))
                        wx.setStorageSync('my_username', user.get('userData').openid)
                        wx.setStorageSync('my_sex', user.get("sex"))
                        wx.setStorageSync('my_avatar', avatarUrl);
                      } catch (e) {
                      }
                      console.log("userid:" + user.id + ",username:" + user.get('userData').openid + " 登录成功");
                    },
                    error: function (user, error) {
                      if (error.code == '101') {
                        var user = new Bmob.User();//开始注册用户
                        user.set('username', userData.openid);
                        user.set('password', userData.openid);
                        user.set("nickname", nickName);
                        user.set("userPic", avatarUrl);
                        user.set("userData", userData);
                        user.set('sex', sex);
                        user.set('feednum', 0);
                        that.signUp(user);
                      }
                    }
                  });
                },
                error: function (error) {
                  console.log("Error: " + error.code + " " + error.message);
                }
              });
            } else {
              console.log('获取用户登录态失败1！' + res.errMsg)
            }
          },
          complete: function (e) {
            console.log('获取用户登录态失败2！' + e)
          }
        });
      }
    } catch (e) {
      console.log("登陆失败")
    } finally{
      that.setData({
        my_nick: userInfo.nickName,
        my_sex: userInfo.gender,
        my_avatar: userInfo.avatarUrl
      })
    }
  },
  signUp: function (user) {
    var that = this;
    user.signUp(null, {
      success: function (result) {
        console.log('注册成功');
        user.set('signUpSuccess', true);
        try {//将返回的3rd_session存储到缓存中
          wx.setStorageSync('user_openid', user.get('userData').openid)
          wx.setStorageSync('user_id', user.id)
          wx.setStorageSync('my_nick', user.get("nickname"))
          wx.setStorageSync('my_username', user.get("username"))
          wx.setStorageSync('my_sex', user.get("sex"))
          wx.setStorageSync('my_avatar', user.get("userPic"));

          //准备就绪,进入下一页
          that.navigateToNextPage();
        } catch (e) {
        }
      },
      error: function (userData, error) {
        console.log("openid=" + userData);
        console.log(error)
      }
    });
  },
  onShareAppMessage: function () {
    return {
      title: "爱，就要在一起",
      path: '/pages/start/start?page=user',
      success: function (res) {
        wx.showToast({
          title: '转发成功',
          icon: 'success'
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '转发失败',
          icon: 'fail'
        });
      }
    }
  },
 
})